package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /*
    * 新增菜品
    * */
    @Transactional(rollbackFor = Exception.class)
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> list = dishDTO.getFlavors();
        if (!list.isEmpty() && list != null){
            list.forEach(i->i.setDishId(dish.getId()));
            dishFlavorMapper.insert(list);
        }
    }

    /*
    * 菜品分页查询
    * */
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageSelect(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total,result);
    }

    /*
    * 批量删除菜品
    * */
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        Integer onSaleCount = dishMapper.onSaleCount(ids);
        Integer haveDishIdCount = setmealDishMapper.dishIdCount(ids);
        if (onSaleCount > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }else if(haveDishIdCount > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.delete(ids);
        dishFlavorMapper.delete(ids);
    }

    /*
    * 根据id查询菜品
    * */
    public DishVO getDishById(Long id) {
        DishVO dishVO =  dishMapper.getDishById(id);
        List<DishFlavor> flavors =  dishFlavorMapper.getDishFlavor(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /*
    * 修改菜品
    * */
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<Long> ids = new ArrayList<>();
        ids.add(dishDTO.getId());
        List<DishFlavor> list = dishDTO.getFlavors();
        list.forEach(item->item.setDishId(dish.getId()));
        if (list != null && list.size() > 0) {
            dishFlavorMapper.delete(ids);
        }
        dishMapper.update(dish);
        if (!list.isEmpty()) dishFlavorMapper.insert(list);
    }

    /*
    * 菜品起售、停售
    * */
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        // TODO 套餐停售
        dishMapper.update(dish);
    }

    /*
    * 根据分类id查询菜品
    * */
    public List<DishVO> getDishByCategoryId(Integer categoryId) {
        List<DishVO> list = dishMapper.selectDishByCategoryId(categoryId, StatusConstant.ENABLE);
        list.forEach(item->{
            List<DishFlavor> dishFlavor = dishFlavorMapper.getDishFlavor(item.getId());
            item.setFlavors(dishFlavor);
        });
        return list;
    }

    /*
     * 根据套餐id查询包含的菜品
     * */
    public List<DishItemVO> getDishBySetmealId(Long setmealId) {
        List<DishItemVO> list = setmealDishMapper.selectBySetmealIdInfo(setmealId);
        return list;
    }
}
